package ignoreruleoperationlogic

import (
	"context"

	"dfx_cmdb/cmdb-blacklist-rpc/internal/svc"
	cmdb_blacklist_rpc "dfx_cmdb/cmdb-blacklist-rpc/types/cmdb-blacklist-rpc"
	cmdbblacklist "dfx_cmdb/models/cmdb-blacklist"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetAllIgnoreRuleLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetAllIgnoreRuleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAllIgnoreRuleLogic {
	return &GetAllIgnoreRuleLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 获取所有忽略规则列表
func (l *GetAllIgnoreRuleLogic) GetAllIgnoreRule(in *cmdb_blacklist_rpc.NullMessageRequest) (*cmdb_blacklist_rpc.QueryAccountInfoResponse, error) {
	// 查询所有忽略规则
	var rules []cmdbblacklist.BlacklistIgnoreRule
	if err := l.svcCtx.PDB.Find(&rules).Error; err != nil {
		l.Errorf("查询忽略规则失败: %v", err)
		return &cmdb_blacklist_rpc.QueryAccountInfoResponse{}, err
	}

	// 转换为响应格式
	var result []*cmdb_blacklist_rpc.UpdateIgnoreRuleRequest
	for _, rule := range rules {
		enabled := false
		if rule.Enable != nil && *rule.Enable {
			enabled = true
		}

		result = append(result, &cmdb_blacklist_rpc.UpdateIgnoreRuleRequest{
			Id:         int32(rule.ID),
			IgnoreType: rule.IgnoreType,
			IgnoreVal:  rule.IgnoreVal,
			Enabled:    enabled,
		})
	}

	return &cmdb_blacklist_rpc.QueryAccountInfoResponse{
		UpdateIgnoreRuleRequest: result,
	}, nil
}
